﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Tools
{
    public class StringHelper
    {
        /// <summary>
        /// 替换回车换行符
        /// </summary>
        /// <param name="str">要替换的字符串</param>
        /// <returns></returns>
        public static string ReplaceNewLine(string str)
        {
            if (string.IsNullOrWhiteSpace(str))
                return str;
            return str.Replace("\n", "").Replace("\r", "");
        }

        /// <summary>
        /// 替换转义字符\r\n\t\s
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string ReplaceEscapeChar(string str)
        {
            if (string.IsNullOrEmpty(str))
                return str;
            str = str.Replace("\n", " ").Replace("\r", " ").Replace("\t", " ");
            return System.Text.RegularExpressions.Regex.Replace(str, "\\s+", " ");
        }

        /**
	 * <PRE>  
	 * 删除中文和英文，中文和数字,中文和中文,数字和数字之间的空格,标点符号和其他所有字符之间的空格
	 * 保留 英文和英文，数字和英文之间的空格
	 * </PRE> 
	 */
        public static String DelSpace(String str)
        {
            if (string.IsNullOrEmpty(str))
                return "";
            char[] char_str = str.ToCharArray();
            if (char_str.Count() == 1)
                return str;

            String del_str = "";
            del_str = del_str + char_str[0];

            for (int i = 1; i < char_str.Length - 1; i++)
            {
                char midd = char_str[i];
                char front = char_str[i + 1];
                char back = char_str[i - 1];

                if (CharOpera.isSpace(midd))
                {
                    //if (CharOpera.isDigit(front) && CharOpera.isDigit(back))
                    //{
                    //    del_str = del_str + char_str[i];
                    //}

                    if (CharOpera.isEngish(front) && CharOpera.isEngish(back))
                    {
                        del_str = del_str + char_str[i];
                    }

                    if (CharOpera.isDigit(front) && CharOpera.isEngish(back))
                    {
                        del_str = del_str + char_str[i];
                    }

                    if (CharOpera.isDigit(back) && CharOpera.isEngish(front))
                    {
                        del_str = del_str + char_str[i];
                    }
                }
                else
                {
                    del_str = del_str + char_str[i];
                }
            }
            del_str = del_str + char_str[char_str.Length - 1];

            return del_str.Trim();
        }

        /**
  * <PRE>  
  * 删除中文和英文，中文和数字,中文和中文,数字和数字之间的空格,标点符号和其他所有字符之间的空格
  * 保留 英文和英文，数字和英文之间的空格,数字和数字之间空格
  * </PRE> 
  */
        public static String DelSpaceNoNum(String str)
        {
            if (string.IsNullOrEmpty(str))
                return "";
            char[] char_str = str.ToCharArray();
            if (char_str.Count() == 1)
                return str;

            String del_str = "";
            del_str = del_str + char_str[0];

            for (int i = 1; i < char_str.Length - 1; i++)
            {
                char midd = char_str[i];
                char front = char_str[i + 1];
                char back = char_str[i - 1];

                if (CharOpera.isSpace(midd))
                {

                    if (CharOpera.isEngish(front) && CharOpera.isEngish(back))
                    {
                        del_str = del_str + char_str[i];
                    }

                    if (CharOpera.isDigit(front) && CharOpera.isEngish(back))
                    {
                        del_str = del_str + char_str[i];
                    }

                    if (CharOpera.isDigit(back) && CharOpera.isEngish(front))
                    {
                        del_str = del_str + char_str[i];
                    }
                    if (CharOpera.isDigit(front) && CharOpera.isDigit(back))
                    {
                        del_str = del_str + char_str[i];
                    }

                }
                else
                {
                    del_str = del_str + char_str[i];
                }
            }
            del_str = del_str + char_str[char_str.Length - 1];

            return del_str.Trim();
        }




        /// <summary>
        /// 获取引文 字符的类型
        /// </summary>
        /// <param name="clean_refertext"></param>
        /// <returns></returns>
        public static string GetStrType(string clean_refertext)
        {
            string strtype = "";
            if (clean_refertext.Contains("[J]"))
            {
                strtype = "J";
            }
            else if (clean_refertext.Contains("[M]"))
            {

                strtype = "M";

            }
            else if (clean_refertext.Contains("[C]"))
            {

                strtype = "C";
            }
            else if (clean_refertext.Contains("[R]"))
            {

                strtype = "R";
            }
            else if (clean_refertext.Contains("[D]"))
            {

                strtype = "D";
            }
            else if (clean_refertext.Contains("[N]"))
            {

                strtype = "N";
            }
            else if (clean_refertext.Contains("[S]"))
            {

                strtype = "S";
            }
            else if (clean_refertext.Contains("[P]"))
            {

                strtype = "P";
            }
            else if (clean_refertext.Contains("[Z]"))
            {

                strtype = "Z";
            }
            else if (clean_refertext.Contains("[DB]"))
            {

                strtype = "DB";
            }
            else if (clean_refertext.Contains("[EB]"))
            {

                strtype = "EB";
            }
            else if (clean_refertext.Contains("[CP]"))
            {
                strtype = "CP";
            }
            else if (clean_refertext.Contains("[ZS]"))
            {
                strtype = "ZS";
            }
            else if (clean_refertext.Contains("[M/CD]"))
            {

                strtype = "M/CD";
            }
            else if (clean_refertext.Contains("[J/CD]"))
            {

                strtype = "J/CD";
            }
            else if (clean_refertext.Contains("[DB/CD]"))
            {
                strtype = "DB/CD";
            }
            else if (clean_refertext.Contains("[CP/DK]"))
            {
                strtype = "CP/DK";
            }
            else if (clean_refertext.Contains("[M/OL]"))
            {
                strtype = "M/OL";
            }
            else if (clean_refertext.Contains("[J/OL]"))
            {

                strtype = "J/OL";
            }
            else if (clean_refertext.Contains("[EB/OL]"))
            {
                strtype = "EB/OL";
            }
            else if (clean_refertext.Contains("[N/OL]"))
            {
                strtype = "N/OL";
            }
            else if (clean_refertext.Contains("[P/OL]"))
            {
                strtype = "P/OL";
            }
            else if (clean_refertext.Contains("[S/OL]"))
            {
                strtype = "S/OL";
            }
            else if (clean_refertext.Contains("[DB/OL]"))
            {
                strtype = "DB/OL";
            }
            else if (clean_refertext.Contains("[CP/OL]"))
            {
                strtype = "CP/OL";
            }
            else if (clean_refertext.Contains("[OL]"))
            {
                strtype = "OL";
            }
            else if (clean_refertext.Contains("[J]"))
            {

                strtype = "J";
            }
            else if (clean_refertext.Contains("[M]"))
            {

                strtype = "M";
            }
            else if (clean_refertext.Contains("[D]"))
            {

                strtype = "D";
            }
            else
            {
                strtype = "K";
            }
            return strtype;
        }

        /// <summary>
        /// 中文标点符号转成英文
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string CnToEnSymbol(string str)
        {
            if (string.IsNullOrEmpty(str)) return "";
            string result = str.Replace("。", ".").Replace("，", ",").Replace("；", ";").Replace("！", "!").Replace("（", "(");
            return result;
        }


        /// <summary>
        /// 判断结尾有没有句号. 没有则加上
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string AddDot(string str)
        {
            char douHao = '.';
            string newstr;
            if (string.IsNullOrEmpty(str) || string.IsNullOrWhiteSpace(str)) return "";
            char c = str[str.Length - 1];
            if (c == douHao)
                return str;
            else if (c == '。')
            {
                newstr = str.Substring(0, str.Length - 1) + ".";
            }
            else
            {
                newstr = str + ".";
            }
            return newstr;
        }


        /// <summary>
        /// 截取字符串长度,加上传入的字符
        /// </summary>
        /// <param name="str"></param>
        /// <param name="endSybol"></param>
        /// <returns></returns>
        public static string Substr(string str, int length, string endSybol)
        {
            if (string.IsNullOrEmpty(str)) return "";

            if (str.Length <= length)
            {
                return str;
            }
            return str.Substring(0, length) + endSybol;
        }



        /// <summary>
        /// 删除最后结尾的一个逗号
        /// </summary>
        public static string DelLastComma(string str)
        {
            if (string.IsNullOrEmpty(str))
            {
                return str;
            }
            return DelLastChar(str, ",");
        }


        /// <summary>
        /// 删除最后结尾的指定字符后的字符
        /// </summary>
        public static string DelLastChar(string str, string strchar)
        {
            if (str.LastIndexOf(strchar) == -1)
            {
                return str;
            }
            return str.Substring(0, str.LastIndexOf(strchar));
        }

        /// <summary>
        /// 删除最后一个相同的字符
        /// </summary>
        public static string DelLastCharBySymop(string str, char strchar)
        {

            if (string.IsNullOrEmpty(str))
            {
                return str;
            }
            if (str[str.Length - 1] == strchar)
            {
                return str.Substring(0, str.Length - 1);
            }
            return str;

        }

        /// <summary>
        /// 把字符串按照分隔符转换成 List
        /// </summary>
        /// <param name="str">源字符串</param>
        /// <param name="speater">分隔符</param>
        /// <param name="toLower">是否转换为小写</param>
        /// <returns></returns>
        public static List<string> GetStrArray(string str, char speater, bool toLower)
        {
            List<string> list = new List<string>();
            string[] ss = str.Split(speater);
            foreach (string s in ss)
            {
                if (!string.IsNullOrEmpty(s) && s != speater.ToString())
                {
                    string strVal = s;
                    if (toLower)
                    {
                        strVal = s.ToLower();
                    }
                    list.Add(strVal);
                }
            }
            return list;
        }
        /// <summary>
        /// 把字符串转 按照, 分割 换为数据
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string[] GetStrArray(string str)
        {
            return str.Split(new Char[] { ',' });
        }
        /// <summary>
        /// 把 List<string> 按照分隔符组装成 string
        /// </summary>
        /// <param name="list"></param>
        /// <param name="speater"></param>
        /// <returns></returns>
        public static string GetArrayStr(List<string> list, string speater)
        {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < list.Count; i++)
            {
                if (i == list.Count - 1)
                {
                    sb.Append(list[i]);
                }
                else
                {
                    sb.Append(list[i]);
                    sb.Append(speater);
                }
            }
            return sb.ToString();
        }
        /// <summary>
        /// 得到数组列表以逗号分隔的字符串
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public static string GetArrayStr(List<int> list)
        {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < list.Count; i++)
            {
                if (i == list.Count - 1)
                {
                    sb.Append(list[i].ToString());
                }
                else
                {
                    sb.Append(list[i]);
                    sb.Append(",");
                }
            }
            return sb.ToString();
        }
        /// <summary>
        /// 得到数组列表以逗号分隔的字符串
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public static string GetArrayValueStr(Dictionary<int, int> list)
        {
            StringBuilder sb = new StringBuilder();
            foreach (KeyValuePair<int, int> kvp in list)
            {
                sb.Append(kvp.Value + ",");
            }
            if (list.Count > 0)
            {
                return DelLastComma(sb.ToString());
            }
            else
            {
                return "";
            }
        }

        /// <summary>
        /// int 转成 2位数的 
        /// </summary>
        /// <param name="num"></param>
        /// <returns></returns>
        public static string GetNum2(int num)
        {
            if (num >= 10)
            {
                return num.ToString();
            }
            else
            {
                return "0" + num;
            }
        }

        /// <summary>
        /// 获取3位数的 卷，或期
        /// </summary>
        /// <param name="num"></param>
        /// <returns></returns>
        public static string GetNum(string num)
        {
            if (string.IsNullOrEmpty(num))
            {
                return "000";
            }
            else
            {
                if (num.Length == 1)
                {
                    return "00" + num;
                }
                else if (num.Length == 2)
                {
                    return "0" + num;
                }
                else
                {
                    return num;
                }
            }
        }

        /// <summary>
        /// 获取4位数
        /// </summary>
        /// <param name="num"></param>
        /// <returns></returns>
        public static string GetNum4(string num)
        {
            if (string.IsNullOrEmpty(num))
            {
                return "0000";
            }
            else
            {
                if (num.Length == 1)
                {
                    return "000" + num;
                }
                else if (num.Length == 2)
                {
                    return "00" + num;
                }
                else if (num.Length == 3)
                {
                    return "0" + num;
                }
                else
                {
                    return num;
                }
            }
        }

        /// <summary>
        /// 获取5位数
        /// </summary>
        /// <param name="num"></param>
        /// <returns></returns>
        public static string GetNum5(string num)
        {
            if (string.IsNullOrEmpty(num))
            {
                return "00000";
            }
            else
            {
                if (num.Length == 1)
                {
                    return "0000" + num;
                }
                else if (num.Length == 2)
                {
                    return "000" + num;
                }
                else if (num.Length == 3)
                {
                    return "00" + num;
                }
                else if (num.Length == 4)
                {
                    return "0" + num;
                }
                else
                {
                    return num;
                }
            }
        }

        /// <summary>
        /// 字符串 原位相加 前面自动补充0
        /// </summary>
        /// <param name="num"></param>
        /// <returns></returns>
        public static string AddStringInt(string str, int num)
        {
            if (!string.IsNullOrEmpty(str))
            {
                long i = 0;
                long.TryParse(str, out i);
                if (0 == i)
                {
                    return "0";
                }

                int leng = str.Length;
                long sn = long.Parse(str);
                sn += num;

                string nstr = sn.ToString();
                while (nstr.Length < leng)
                {
                    nstr = "0" + nstr;
                }
                return nstr;
            }
            return str;
        }



    }


    /// <summary>
    /// 检测字符是不是 数字，字母，中文,标点等
    /// </summary>
    public class CharOpera
    {
        /**  
         * <PRE>  
         * 判断字符是否是中文字符
         * </PRE>  
         */
        public static bool isChinese(char c)
        {
            if (c >= 0x4E00 && c <= 0x9FA5)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /**  
         * <PRE>  
         * 判断字符是否是英文字符
         * </PRE>  
         */
        public static bool isEngish(char c)
        {
            int b = c + 0;
            if ((b >= 65 && b <= 90) || (b >= 97 && b <= 122))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 判断字符是否是数字字符
        /// </summary>
        /// <param name="c"></param>
        /// <returns></returns>
        public static bool isDigit(char c)
        {
            if (c >= '0' && c <= '9')
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 判断字符是否是空格
        /// </summary>
        /// <param name="c"></param>
        /// <returns></returns>
        public static bool isSpace(char c)
        {
            int s = c + 0;
            if (s == 32) return true;
            if (c == 0x0020 || c == 0x3000) return true;
            return false;
        }

        /// <summary>
        /// 判断字符是否是符号
        /// </summary>
        /// <param name="ch"></param>
        /// <returns></returns>
        public static bool isSymbol(char ch)
        {
            if (char.IsSymbol(ch)) return true;
            return false;
            //if (isCnSymbol(ch)) return true;
            //if (isEnSymbol(ch)) return true;

            //if (0x2010 <= ch && ch <= 0x2017) return true;
            //if (0x2020 <= ch && ch <= 0x2027) return true;
            //if (0x2B00 <= ch && ch <= 0x2BFF) return true;
            //if (0xFF03 <= ch && ch <= 0xFF06) return true;
            //if (0xFF08 <= ch && ch <= 0xFF0B) return true;
            //if (ch == 0xFF0D || ch == 0xFF0F) return true;
            //if (0xFF1C <= ch && ch <= 0xFF1E) return true;
            //if (ch == 0xFF20 || ch == 0xFF65) return true;
            //if (0xFF3B <= ch && ch <= 0xFF40) return true;
            //if (0xFF5B <= ch && ch <= 0xFF60) return true;
            //if (ch == 0xFF62 || ch == 0xFF63) return true;
            ////      if(ch == 0x0020 || ch == 0x3000) return true;//表示空格
            //return false;
        }

        /// <summary>
        /// 判断字符是否是中文符号
        /// </summary>
        /// <param name="ch"></param>
        /// <returns></returns>
        public static bool isCnSymbol(char ch)
        {
            if (0x3004 <= ch && ch <= 0x301C) return true;
            if (0x3020 <= ch && ch <= 0x303F) return true;
            return false;
        }

        /// <summary>
        /// 判断字符是否是英文符号
        /// </summary>
        /// <param name="ch"></param>
        /// <returns></returns>
        public static bool isEnSymbol(char ch)
        {
            if (ch == 0x40) return true;
            if (ch == 0x2D || ch == 0x2F) return true;
            if (0x23 <= ch && ch <= 0x26) return true;
            if (0x28 <= ch && ch <= 0x2B) return true;
            if (0x3C <= ch && ch <= 0x3E) return true;
            if (0x5B <= ch && ch <= 0x60) return true;
            if (0x7B <= ch && ch <= 0x7E) return true;
            return false;
        }


        /// <summary>
        /// 判断字符是否是标点
        /// </summary>
        /// <param name="ch"></param>
        /// <returns></returns>
        public static bool isPunctuation(char ch)
        {
            if (isCjkPunc(ch)) return true;
            if (isEnPunc(ch)) return true;

            if (0x2018 <= ch && ch <= 0x201F) return true;
            if (ch == 0xFF01 || ch == 0xFF02) return true;
            if (ch == 0xFF07 || ch == 0xFF0C) return true;
            if (ch == 0xFF1A || ch == 0xFF1B) return true;
            if (ch == 0xFF1F || ch == 0xFF61) return true;
            if (ch == 0xFF0E) return true;
            if (ch == 0xFF65) return true;
            return false;
        }

        /// <summary>
        /// 判断字符是否是英文标点
        /// </summary>
        /// <param name="ch"></param>
        /// <returns></returns>
        public static bool isEnPunc(char ch)
        {
            if (0x21 <= ch && ch <= 0x22) return true;
            if (ch == 0x27 || ch == 0x2C) return true;
            if (ch == 0x2E || ch == 0x3A) return true;
            if (ch == 0x3B || ch == 0x3F) return true;
            return false;
        }

        /// <summary>
        /// 判断字符是否是中文标点
        /// </summary>
        /// <param name="ch"></param>
        /// <returns></returns>
        public static bool isCjkPunc(char ch)
        {
            if (isSymbol(ch)) return true;
            return false;
        }

        /**  
         * <PRE>  
         * 判断字符是否是标点或者符号
         * </PRE>  
         */
        public static bool isSymPunc(char ch)
        {
            if (char.IsSymbol(ch))
            {
                return true;
            }
            if (char.IsPunctuation(ch))
            {
                return true;
            }
            return false;
        }




    }
}